package com.fcscs.cservice.dao.hibernate;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.fcscs.core.dao.ConnectionManager;
import com.fcscs.core.domain.BaseEntity.DBLoc;
import com.fcscs.cservice.common.CServiceCommonEnums.ReservationStatus;
import com.fcscs.cservice.dao.CServiceCommonDao;
import com.fcscs.cservice.dao.ReservationDao;
import com.fcscs.cservice.domain.Folio;
import com.fcscs.cservice.domain.Message;
import com.fcscs.cservice.domain.RC;
import com.fcscs.cservice.domain.ReservationBase;
import com.fcscs.cservice.domain.model.BillingView;
import com.fcscs.cservice.domain.model.BillingViewForFile;
import com.fcscs.cservice.domain.model.FolioView;
import com.fcscs.cservice.domain.model.FolioViewForFile;
import com.fcscs.cservice.domain.model.MessageView;
import com.fcscs.cservice.domain.model.RCView;
import com.fcscs.cservice.domain.model.RCViewForFile;
import com.fcscs.cservice.domain.model.RateView;
import com.fcscs.cservice.domain.model.RateViewForFile;
import com.fcscs.cservice.domain.model.ReservationView;
import com.fcscs.share.util.TimezoneUtils;
import java.util.Calendar;

@Repository("reservationDao")
public class ReservationDaoHibernate implements ReservationDao {

	@Autowired
	private ConnectionManager connMgr;	
	@Autowired
	private CServiceCommonDao ccDao;

	private static DBLoc RSVDBLoc = new ReservationBase().getDBLoc();

	private static final String FIND_RSV = " from ReservationView rv where rv.propertyId = :propId ";
	private static final String FIND_RSV_CNT = "select count(id) from ReservationView rv where rv.propertyId = :propId ";
	private static final String BY_TERM = " and (rv.guestFirstName like :term or rv.guestLastName like :term or confirmationNo like :term) ";
	private static final String BY_RSV_STAT = " and rv.reservationStatusId = :rsvStatId ";
	private static final String BY_TIME_RANGE = " and rv.arrivalDateTime between :startTime and :endTime ";

	private static final String AFIND_RSV = " select distinct rv.* from reservation_v rv ";
	private static final String AFIND_RSV_CNT = " select count(distinct rv.id) from reservation_v rv ";
	private static final String AFIND_JOIN_MEMB = " inner join membership mb on mb.guest_profile_id = rv.guest_profile_id ";
	private static final String AFIND_WHERE = " where rv.property_id = :propId ";
	private static final String AFIND_BY_RSV_STAT = " and rv.reservation_status_id = :rsvStatId ";
	private static final String AFIND_BY_FIRST_NAME = " and rv.guest_firstname like :firstName ";
	private static final String AFIND_BY_LAST_NAME = " and rv.guest_lastname like :lastName ";
	private static final String AFIND_BY_ROOM_NO = " and rv.room_number like :roomNo ";
	private static final String AFIND_BY_ROOM_STATUS = " and rv.room_status_id = :roomStatId ";
	private static final String AFIND_BY_BAGGAGE_NO = " and rv.baggage_number like :bagNo ";
	private static final String AFIND_BY_ARV_DATE = " and rv.arrival_date_time between :arvStart and :arvEnd ";
	private static final String AFIND_BY_DPT_DATE = " and rv.departure_date_time between :dptStart and :dptEnd ";
	private static final String AFIND_BY_MEMB = " and mb.number like :memNo ";

	private static final String GET_RSV_VIEW = " from ReservationView rv where rv.id = :id ";
	private static final String GET_RSV_VIEW_BY_GP = " select * from reservation_v rv where rv.guest_profile_id = :gpId " +
			" and rv.reservation_status_id in (:status) order by rv.id desc limit 1 ";

	private static final String GET_RC_VIEW = " select rc.*, d.bill_code direct_bill_code, rsv.arrival_date_time arrival_date_time, " +
			" rsv.departure_date_time departure_date_time, rsv.number_of_guest number_of_guest from rc rc " +
			" left join direct_bill_code d on d.id = rc.direct_bill_code_id " +
			" left join reservation rsv on rc.reservation_id = rsv.id where rc.id = :id ";
	private static final String GET_RC_VIEW_FILE = " select rc.*, d.bill_code direct_bill_code, rsv.arrival_date_time arrival_date_time, " +
			" rsv.departure_date_time departure_date_time, rsv.number_of_guest number_of_guest, pm.method payment_method, pc.tandc tandc from rc rc " +
			" left join direct_bill_code d on d.id = rc.direct_bill_code_id left join payment_method pm on rc.payment_method_id = pm.id " +
			" left join reservation rsv on rc.reservation_id = rsv.id inner join property_configuration pc on rc.property_id = pc.property_id where rc.id = :id ";
	private static final String GET_RCS_BULK = " select id, reservation_id, guest_signature_date_time, status from rc rc " +
			" where rc.reservation_id in (:rsvIds) order by rc.reservation_id, rc.id desc ";
	private static final String GET_RATES = " from RateView rv where rv.rcId = :rcId order by rv.fromDate ";
	private static final String GET_RATES_FILE = " from RateViewForFile rv where rv.rcId = :rcId order by rv.fromDate ";

	private static final String GET_FOLIO_VIEW = " select * from folio_v fv where fv.id = :id ";
	private static final String GET_FOLIO_VIEW_FILE = " select fv.*, pm.method payment_method from folio_v fv " +
			" left outer join payment_method pm on fv.paymethod_id = pm.id where fv.id = :id ";
	private static final String GET_BILLS = " select * from billing b where b.folio_id = :folioId order by b.billing_date_time ";

	private static final String GET_MSGS = " select * from message m where m.reservation_id = :rsvId order by m.msg_date_time desc ";
	private static final String GET_MSGS_BY_IDS = " from Message m where id in (:ids) ";

	static final SimpleDateFormat DF = new SimpleDateFormat("yyyy-MM-dd");
	static final SimpleDateFormat TF = new SimpleDateFormat("HH:mm:ss");

	@SuppressWarnings("unchecked")
	@Override
	public List<ReservationView> findReservations(long propId, String term, Long rsvStatusId, Date startTime, Date endTime,
			int start, int resultCnt) {
		Query q = buildSimpleRsvSearch(propId, term, rsvStatusId, startTime, endTime, true);
		q.setMaxResults(resultCnt);
		q.setFirstResult(start);
		List<ReservationView> result = q.list();
		for (ReservationView rv : result) {
			setStatus(rv);
		}
		return result;
	}

	@Override
	public int findReservationsCnt(long propId, String term, Long rsvStatusId, Date startTime, Date endTime) {
		Query q = buildSimpleRsvSearch(propId, term, rsvStatusId, startTime, endTime, false);
		return ((Number) q.uniqueResult()).intValue();
	}

	private Query buildSimpleRsvSearch(long propId, String term, Long rsvStatusId, Date startTime, Date endTime, boolean isSearch) {
		if (startTime == null) {
			startTime = TimezoneUtils.setDateStartTime(new Date());
		}
		if (endTime == null) {
			endTime = TimezoneUtils.setDateEndTime(new Date());
		}
		term = trimStringCond(term);
		Query q = connMgr.getCurrentSession(RSVDBLoc).createQuery((isSearch ? FIND_RSV : FIND_RSV_CNT)
				+ (term != null ? BY_TERM : "")
				+ (rsvStatusId != null ? BY_RSV_STAT : "") + BY_TIME_RANGE);
		q.setLong("propId", propId);
		if (term != null) {
			q.setString("term",  term + "%");
		}
		if (rsvStatusId != null) {
			q.setLong("rsvStatId", rsvStatusId);
		}
		q.setTimestamp("startTime", startTime);
		q.setTimestamp("endTime", endTime);
		return q;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<ReservationView> findReservations(long propId, String firstName, String lastName, Long rsvStatus, String roomNo,
			String memberNo, Long roomStatus, String baggageNo, Date arrivDate, Date departDate, Date eta, Date etd,
			Date startTime, Date endTime, int start, int resultCnt) {
		Query q = buildAdvancedRsvSearch(propId, firstName, lastName, rsvStatus, roomNo, memberNo, roomStatus, baggageNo,
				arrivDate, departDate, eta, etd, startTime, endTime, true);
		q.setMaxResults(resultCnt);
		q.setFirstResult(start);
		List<ReservationView> result = q.list();
		for (ReservationView rv : result) {
			setStatus(rv);
		}
		return result;
	}

	@Override
	public int findReservationsCnt(long propId, String firstName, String lastName, Long rsvStatus, String roomNo, String memberNo,
			Long roomStatus, String baggageNo, Date arrivDate, Date departDate, Date eta, Date etd, Date startTime, Date endTime) {
		Query q = buildAdvancedRsvSearch(propId, firstName, lastName, rsvStatus, roomNo, memberNo, roomStatus, baggageNo,
				arrivDate, departDate, eta, etd, startTime, endTime, false);
		return ((Number) q.uniqueResult()).intValue();
	}

	private Query buildAdvancedRsvSearch(long propId, String firstName, String lastName, Long rsvStatus, String roomNo, String memberNo,
			Long roomStatus, String baggageNo, Date arrivDate, Date departDate, Date eta, Date etd, Date startTime, Date endTime, boolean isSearch) {
		firstName = trimStringCond(firstName);
		lastName = trimStringCond(lastName);
		roomNo = trimStringCond(roomNo);
		memberNo = trimStringCond(memberNo);
		baggageNo = trimStringCond(baggageNo);
		
		StringBuffer sb = new StringBuffer(isSearch ? AFIND_RSV : AFIND_RSV_CNT);
		sb.append(memberNo == null ? "" : AFIND_JOIN_MEMB).append(AFIND_WHERE)
			.append(firstName == null ? "" : AFIND_BY_FIRST_NAME).append(lastName == null ? "" : AFIND_BY_LAST_NAME)
			.append(rsvStatus == null ? "" : AFIND_BY_RSV_STAT).append(roomNo == null ? "" : AFIND_BY_ROOM_NO)
			.append(roomStatus == null ? "" : AFIND_BY_ROOM_STATUS).append(baggageNo == null ? "" : AFIND_BY_BAGGAGE_NO)
			.append(memberNo == null ? "" : AFIND_BY_MEMB);
		if (arrivDate == null && departDate == null ) {
			if (startTime == null) {
				startTime = TimezoneUtils.setDateStartTime(new Date());
			}
			if (endTime == null) {
				endTime = TimezoneUtils.setDateEndTime(new Date());
			}
			int searchDays = ccDao.getRsvSearchDays(propId);
			Calendar cal = Calendar.getInstance();
			cal.setTime(startTime);
			cal.add(Calendar.DAY_OF_MONTH, -searchDays);
			startTime = cal.getTime();
			cal.setTime(endTime);
			cal.add(Calendar.DAY_OF_MONTH, searchDays);
			endTime = cal.getTime();
			sb.append(AFIND_BY_ARV_DATE);
		} else {
			sb.append(arrivDate == null ? "" : AFIND_BY_ARV_DATE).append(departDate == null ? "" : AFIND_BY_DPT_DATE);
		}

		SQLQuery q = connMgr.getCurrentSession(RSVDBLoc).createSQLQuery(sb.toString());
		q.setLong("propId", propId);
		if (firstName != null) {
			q.setString("firstName", firstName + "%");
		}
		if (lastName != null) {
			q.setString("lastName", lastName + "%");
		}
		if (rsvStatus != null) {
			q.setLong("rsvStatId", rsvStatus);
		}
		if (roomNo != null) {
			q.setString("roomNo", roomNo + "%");
		}
		if (roomStatus != null) {
			q.setLong("roomStatId", roomStatus);
		}
		if (baggageNo != null) {
			q.setString("bagNo", baggageNo + "%");
		}
		if (memberNo != null) {
			q.setString("memNo", memberNo +"%");
		}
		if (arrivDate == null && departDate == null) {
			q.setTimestamp("arvStart", startTime);
			q.setTimestamp("arvEnd", endTime);
		} else {
			if (arrivDate != null){
				Calendar c = Calendar.getInstance();
				c.setTime(arrivDate);
				if (eta != null) {
					c.add(Calendar.SECOND, 59);
				} else {
					c.add(Calendar.HOUR_OF_DAY, 24);
					c.add(Calendar.SECOND, -1);
				}
				q.setTimestamp("arvStart", arrivDate);
				q.setTimestamp("arvEnd", c.getTime());
			}
			if (departDate != null) {
				Calendar c = Calendar.getInstance();
				c.setTime(departDate);
				if (etd != null) {
					c.add(Calendar.SECOND, 59);
				} else {
					c.add(Calendar.HOUR_OF_DAY, 24);
					c.add(Calendar.SECOND, -1);
				}
				q.setTimestamp("dptStart", departDate);
				q.setTimestamp("dptEnd", c.getTime());
			}
		}
		if (isSearch) {
			q.addEntity(ReservationView.class);
		}
		return q;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ReservationView> findRsvForFolio(long propId, String firstName, String lastName, Long rsvStatusId, String roomNo,
			Date arrivStart, Date arrivEnd, Date departStart, Date departEnd, Date date, int start, int resultCnt) {
		Query q = buildRsvForFolioSearch(propId, firstName, lastName, rsvStatusId, roomNo, arrivStart, arrivEnd, departStart, departEnd, date, true);
		q.setMaxResults(resultCnt);
		q.setFirstResult(start);
		List<ReservationView> result = q.list();
		for (ReservationView rv : result) {
			setStatus(rv);
		}
		return q.list();
	}

	@Override
	public int findRsvForFolioCnt(long propId, String firstName, String lastName, Long rsvStatusId, String roomNo,
			Date arrivStart, Date arrivEnd, Date departStart, Date departEnd, Date date) {
		Query q = buildRsvForFolioSearch(propId, firstName, lastName, rsvStatusId, roomNo, arrivStart, arrivEnd, departStart, departEnd, date, false);
		return ((Number) q.uniqueResult()).intValue();
	}

	private Query buildRsvForFolioSearch(long propId, String firstName, String lastName, Long rsvStatusId, String roomNo,
			Date arrivStart, Date arrivEnd, Date departStart, Date departEnd, Date date, boolean isSearch) {
		firstName = trimStringCond(firstName);
		lastName = trimStringCond(lastName);
		roomNo = trimStringCond(roomNo);
		
		StringBuffer sb = new StringBuffer(isSearch ? AFIND_RSV : AFIND_RSV_CNT);
		sb.append(AFIND_WHERE).append(firstName == null ? "" : AFIND_BY_FIRST_NAME).append(lastName == null ? "" : AFIND_BY_LAST_NAME)
			.append(rsvStatusId == null ? "" : AFIND_BY_RSV_STAT).append(roomNo == null ? "" : AFIND_BY_ROOM_NO);
		if (arrivStart != null || arrivEnd != null) {
			sb.append(AFIND_BY_ARV_DATE);
		}
		if (departStart != null || departEnd != null) {
			sb.append(AFIND_BY_DPT_DATE);
		}
		if (arrivStart == null && arrivEnd == null && departStart == null && departEnd == null) {
			sb.append(AFIND_BY_ARV_DATE);
		}

		SQLQuery q = connMgr.getCurrentSession(RSVDBLoc).createSQLQuery(sb.toString());
		q.setLong("propId", propId);
		if (firstName != null) {
			q.setString("firstName", firstName + "%");
		}
		if (lastName != null) {
			q.setString("lastName", lastName + "%");
		}
		if (rsvStatusId != null) {
			q.setLong("rsvStatId", rsvStatusId);
		}
		if (roomNo != null) {
			q.setString("roomNo", roomNo + "%");
		}

		Date startTime = date;
		if (startTime == null) {
			startTime = TimezoneUtils.setDateStartTime(new Date());
		}
		Date endTime = startTime;
		int searchDays = -1;
		if (arrivStart == null || departStart == null) {
			searchDays = ccDao.getRsvSearchDays(propId);
			Calendar cal = Calendar.getInstance();
			cal.setTime(startTime);
			cal.add(Calendar.DAY_OF_MONTH, -searchDays);
			startTime = cal.getTime();
		}
		if (arrivEnd == null || departEnd == null) {
			if (searchDays == -1) {
				searchDays = ccDao.getRsvSearchDays(propId);
			}
			Calendar cal = Calendar.getInstance();
			cal.setTime(endTime);
			cal.add(Calendar.DAY_OF_MONTH, searchDays + 1);
			cal.add(Calendar.SECOND, -1);
			endTime = cal.getTime();
		}

		if (arrivStart != null || arrivEnd != null) {
			q.setTimestamp("arvStart", arrivStart != null ? arrivStart : startTime);
			q.setTimestamp("arvEnd", arrivEnd != null ? arrivEnd : endTime);
		}
		if (departStart != null || departEnd != null) {
			q.setTimestamp("dptStart", departStart != null ? departStart : startTime);
			q.setTimestamp("dptEnd", departEnd != null ? departEnd : endTime);
		}
		if (arrivStart == null && arrivEnd == null && departStart == null && departEnd == null) {
			q.setTimestamp("arvStart", startTime);
			q.setTimestamp("arvEnd", endTime);
		}

		if (isSearch) {
			q.addEntity(ReservationView.class);
		}
		return q;
	}

	@Override
	public ReservationView getReservationView(long id) {
		Query q = connMgr.getCurrentSession(RSVDBLoc).createQuery(GET_RSV_VIEW);
		q.setLong("id", id);
		ReservationView result = (ReservationView)q.uniqueResult();
		setStatus(result);
		return result;
	}

	@Override
	public ReservationView getCurReservationViewByGP(long gpId) {
		SQLQuery q = connMgr.getCurrentSession(RSVDBLoc).createSQLQuery(GET_RSV_VIEW_BY_GP);
		q.setLong("gpId", gpId);
		q.setParameterList("status", ReservationStatus.getInRoomStatusList());
		q.addEntity(ReservationView.class);
		ReservationView result = (ReservationView)q.uniqueResult();
		setStatus(result);
		return result;
	}
	
	private void setStatus(ReservationView rv) {
		if (rv == null) {
			return;
		}
		ReservationStatus status = ReservationStatus.getReservationStatus(rv.getReservationStatusId());
		if (status != null) {
			rv.setReservationStatus(status.getStatusStr());
		}
	}

	@Override
	public ReservationBase getReservation(long id) {
		return (ReservationBase)connMgr.getCurrentSession(RSVDBLoc).get(ReservationBase.class, id);
	}

	@Override
	public ReservationBase updateReservation(ReservationBase rsv) {
		Session s = connMgr.getCurrentSession(RSVDBLoc);
		s.saveOrUpdate(rsv);
		return rsv;
	}

	@Override
	public RCView getRCView(long id) {
		SQLQuery q = connMgr.getCurrentSession(RSVDBLoc).createSQLQuery(GET_RC_VIEW);
		q.setLong("id", id);
		q.addEntity(RCView.class);
		RCView rcv = (RCView)q.uniqueResult();
		return rcv;
	}

	@Override
	public RCViewForFile getRCViewForFile(long id) {
		SQLQuery q = connMgr.getCurrentSession(RSVDBLoc).createSQLQuery(GET_RC_VIEW_FILE);
		q.setLong("id", id);
		q.addEntity(RCViewForFile.class);
		RCViewForFile rcv = (RCViewForFile)q.uniqueResult();
		return rcv;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<Long, List<ReservationView.RC>> getRCs(Set<Long> rsvIds) {
		if (rsvIds == null || rsvIds.size() <= 0) {
			return null;
		}
		SQLQuery q = connMgr.getCurrentSession(RSVDBLoc).createSQLQuery(GET_RCS_BULK);
		q.setParameterList("rsvIds", rsvIds);
		q.addEntity(ReservationView.RC.class);
		List<ReservationView.RC> rcList = q.list();
		Map<Long, List<ReservationView.RC>> rcMap = new HashMap<Long, List<ReservationView.RC>>();
		if (rcList != null && rcList.size() > 0) {
			long rsvId = -1;
			List<ReservationView.RC> rcs = null;
			for (ReservationView.RC rc: rcList) {
				if (rcs == null) {
					rsvId = rc.getReservationId();
					rcs = new ArrayList<ReservationView.RC>();
				} else if (rsvId != rc.getReservationId()) {
					rcMap.put(rsvId, rcs);
					rsvId = rc.getReservationId();
					rcs = new ArrayList<ReservationView.RC>();
				}
				rcs.add(rc);
			}
			if (rcs != null) {
				rcMap.put(rsvId, rcs);
			}
		}
		return rcMap;
	}

	@Override
	public RC getRC(long id) {
		return (RC)connMgr.getCurrentSession(RSVDBLoc).get(RC.class, id);
	}

	@Override
	public RC updateRC(RC rc) {
		Session s = connMgr.getCurrentSession(RSVDBLoc);
		s.saveOrUpdate(rc);
		return rc;
	}
	
	@Override
	public Folio updateFolio(Folio folio) {
		Session s = connMgr.getCurrentSession(RSVDBLoc);
		s.saveOrUpdate(folio);
		return folio;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RateView> getRateViews(long rcId) {
		Query q = connMgr.getCurrentSession(RSVDBLoc).createQuery(GET_RATES);
		q.setLong("rcId", rcId);
		return q.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RateViewForFile> getRateViewsForFile(long rcId) {
		Query q = connMgr.getCurrentSession(RSVDBLoc).createQuery(GET_RATES_FILE);
		q.setLong("rcId", rcId);
		return q.list();
	}

	@Override
	public FolioView getFolioView(long id) {
		SQLQuery q = connMgr.getCurrentSession(RSVDBLoc).createSQLQuery(GET_FOLIO_VIEW);
		q.setLong("id", id);
		q.addEntity(FolioView.class);
		return (FolioView)q.uniqueResult();
	}

	@Override
	public FolioViewForFile getFolioViewForFile(long id) {
		SQLQuery q = connMgr.getCurrentSession(RSVDBLoc).createSQLQuery(GET_FOLIO_VIEW_FILE);
		q.setLong("id", id);
		q.addEntity(FolioViewForFile.class);
		return (FolioViewForFile)q.uniqueResult();
	}

	@Override
	public Folio getFolio(long id) {
		return (Folio)connMgr.getCurrentSession(RSVDBLoc).get(Folio.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BillingView> getBillings(long folioId) {
		SQLQuery q = connMgr.getCurrentSession(RSVDBLoc).createSQLQuery(GET_BILLS);
		q.setLong("folioId", folioId);
		q.addEntity(BillingView.class);
		return q.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BillingViewForFile> getBillingsForFile(long folioId) {
		SQLQuery q = connMgr.getCurrentSession(RSVDBLoc).createSQLQuery(GET_BILLS);
		q.setLong("folioId", folioId);
		q.addEntity(BillingViewForFile.class);
		return q.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MessageView> getMessages(long rsvId) {
		SQLQuery q = connMgr.getCurrentSession(RSVDBLoc).createSQLQuery(GET_MSGS);
		q.setLong("rsvId", rsvId);
		q.addEntity(MessageView.class);
		return q.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Message> getMessages(Set<Long> msgIds) {
		Query q = connMgr.getCurrentSession(RSVDBLoc).createQuery(GET_MSGS_BY_IDS);
		q.setParameterList("ids", msgIds);
		return q.list();
	}

	@Override
	public List<Message> updateMessages(List<Message> msgList) {
		for (Message m : msgList) {
			connMgr.getCurrentSession(RSVDBLoc).saveOrUpdate(m);
		}
		return msgList;
	}

//	@Override
//	public boolean expressCheckIn(long rsvId, RCView rc) {
//		// TODO Auto-generated method stub
//		return true;
//	}
//
//	@Override
//	public boolean checkOut(long rsvId, FolioView folio, boolean isAuto) {
//		return true;
//	}
//
//	@Override
//	public boolean sendEFolio(long rsvId, long folioId) {
//		// TODO Auto-generated method stub
//		return true;
//	}

	private String trimStringCond(String cond) {
		if (cond != null) {
			cond = cond.trim();
			if ("".equals(cond)) {
				cond = null;
			}
		}
		return cond;
	}
}
